var $table = $("#roadTab");
$(function () {
    initTable();
    //条件查询
    $("#queryRoadBtn").click(function () {
        $table.bootstrapTable('refresh');
    });
    //增加按钮
    $("#addBut").click(function () {
    	initStationEdit(null);
    });
});

function initTable() {
    $table.bootstrapTable({
        dataType: "json",
        showColumns: true,
        pagination: true, //分页
        striped: true,
        cache: false,	//是否使用缓存，默认是true
        pageSize: 10,
        pageNumber: 1,
        queryParamsType: 'limit',
        smartDisplay: true,
        sidePagination: "server",
        method: "post",	//使用post提交
        queryParams: queryParams,
        url: '../road/getRoadInfoByPage.htm',
        contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
        columns: [
			{
				title: "序号",
			    align: 'center',
			    formatter:function(value,row,index){
			    	return index + 1;
			    }
			},
            {
                title: "路线编号",
                field: "roadNum",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "路线起点名称",
                field: "roadStartName",
                align: 'center',
                valign: 'middle'

            },
            {
                title: "路线终点名称",
                field: "roadEndName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "路线途径站点",
                field: "roadCross",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "路线途径城市",
                field: "roadCrossName",
                align: 'center',
                valign: 'middle'
            },
            {
                title: "是否禁用",
                field: "roadIsDelete",
                align: 'center',
                formatter: function(value, row, index){
            		var isdel = "";
            		if(row.roadIsDelete == 1){
            			isdel = '<span style="color:#ed5565;">已禁用</span>';
            		}else if(row.roadIsDelete == 0){
            			isdel ='<span style="color:#23c6c8;">未禁用</span>';
            		}
            		return isdel;
            	}
            },
            {
                title: "操作",
                align: 'center',
                formatter: function (value, row, index) {
                    var e = '<button class="btn btn-xs btn-info" type="button" onclick="initStationEdit(\'' + row.roadId + '\')"><i class="fa fa-edit"></i> 编辑</button> ';
                    var d = '';
                    if (row.roadIsDelete == 0){
                        d = '<button class="btn btn-xs btn-danger" type="button" onclick="deleteStation(\'' + row.roadId + '\',\'' + row.roadNum + '\',\'' + 1 + '\')"><i class="fa fa-trash-o"></i> 禁用 </button> ';
                    }else if(row.roadIsDelete == 1){
                        d = '<button class="btn btn-xs btn-warning" type="button" onclick="deleteStation(\'' + row.roadId + '\',\'' + row.roadNum + '\',\'' + 0 + '\')"><i class="fa fa-trash-o"></i> 启用 </button> ';
                    }
                    return e + d;
                }
            }
        ]
    });
}

//分页参数，条件查询时的参数
function queryParams(queryParamsType){
	var temp = {
		limit: queryParamsType.limit,   //页面大小
		offset: queryParamsType.offset,  //页码
		roadNum: $.trim($('#roadNum').val()),
		roadStartName: $.trim($('#roadStartName').val()),
		roadEndName: $.trim($('#roadEndName').val()),
		roadIsDelete: $.trim($('#roadIsDelete').val()),
	};
	return temp;
}

//编辑路线信息
function initStationEdit(id){
    var title;
    if(id){
        title = '编辑站点';
    }else{
        title = '新增站点';
    }
    var index = layer.open({
        type: 2,
        title: title,
        maxmin: true,
        shadeClose: false, //点击遮罩关闭层
        area : ['650px','700px'],
        content: '../road/edit_road.htm?id='+id,
        success: function(layero, index){
        }
    });
}

//设置路线状态
function deleteStation(roadId, roadNum, isDelete) {
    layer.confirm('你确定要修改【' + roadNum + '】站点状态吗？', {
        btn: ['修改', '取消'] //按钮
    }, function () {
        $.ajax({
            url: "../road/modifyRoadDisable.htm",
            cache: false,
            type: "post",
            dataType: "json",
            data: {'roadId': roadId,'isDel':isDelete},
            success: function (result) {
                if (result.status == 0) {
                    layer.msg(result.message, {icon: 1});
                    $table.bootstrapTable('refresh', {url: '../road/getRoadInfoByPage.htm'});
                    return;
                }
                layer.msg(result.message, {icon: 2});
            }
        });
    }, function () {
    });
}